using Microsoft.AspNetCore.Mvc;
using MallSystembackend.Api.Repository;
using MallSystembackend.Api.Model;
using Microsoft.Extensions.Configuration;
using MallSystembackend.Api.ParamModel;
using System.Linq;
namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SkuController : ControllerBase
    {
        public IRepository<Sku> _skuRepository;
        public readonly IConfiguration _configuration;

        public SkuController(IRepository<Sku> skuRepository,
        IConfiguration configuration)
        {
            _skuRepository = skuRepository;
            _configuration = configuration;
        }
        //添加商品sku信息
        [HttpPost]
        public ActionResult AddSku(ParamSku model)
        {
            var addsku = new Sku
            {
                parameterName = model.parameterName,
                CommodityId = model.CommodityId
            };
            _skuRepository.Insert(addsku);
            return Ok(new
            {
                Code = 1000,
                Data = addsku,
                Msg = "添加成功"
            });
        }
        //修改商品sku信息
        [HttpPut("{id}")]
        public ActionResult UpdateSku(int id, ParamSku model)
        {
            var UpdateSku = _skuRepository.GetById(id);
            if (UpdateSku != null)
            {
                UpdateSku.parameterName = model.parameterName;
                _skuRepository.Update(UpdateSku);
                return Ok(new
                {
                    Code = 1000,
                    Data = UpdateSku,
                    Msg = "修改成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = "",
                Msg = "修改失败"
            });

        }
        //删除商品sku信息
        [HttpDelete("{id}")]
        public ActionResult DeletedSku(int id)
        {
            var DeleteSku = _skuRepository.GetById(id);
            DeleteSku.IsDeleted = true;
            _skuRepository.Update(DeleteSku);
            return Ok(new
            {
                Code = 1000,
                Data = DeleteSku,
                Msg = "删除成功"
            });
        }
        //根据商品id获得对应sku信息
        [HttpGet("{id}")]
        public ActionResult GetSku(int id)
        {
            var skus = _skuRepository.Table;
            var getsku = skus.Where(x => x.CommodityId == id).Where(x => x.IsDeleted == false);
            return Ok(new
            {
                Code = 1000,
                Data = getsku,
                Msg = "获得成功"
            });
        }
    }
}